package com.craigcmiller.cmtetris.core;


/**
 * Specific block type
 * @author craig
 */
public class TBlock extends Block
{
	private int rotation;
	
	
	/**
	 * Construct the block
	 * @param b
	 */
	public TBlock(Board b)
	{
		super(b);
		
		this.rotation=0;
	}
	
	
	/**
	 * Create the initial version of the block
	 */
	@Override
	protected void build()
	{
		cells.add(new BlockCell(board.getWidth()/2, 0));
		cells.add(new BlockCell(board.getWidth()/2-1, 1));
		cells.add(new BlockCell(board.getWidth()/2, 1));
		cells.add(new BlockCell(board.getWidth()/2+1, 1));
	}
	
	
	/**
	 * Rotate the block
	 */
	@Override
	public void rotate()
	{
		switch (rotation) {
			case 0:
				if (this.isCellMoveLegal(1, 1, 1)) {
					this.moveCell(1, 1, 1);
					
					rotation=1;
				}
				
				break;
			case 1:
				if (this.isCellMoveLegal(0, -1, 1)) {
					this.moveCell(0, -1, 1);
					
					rotation=2;
				}
				
				break;
			case 2:
				if (this.isCellMoveLegal(3, -1, -1)) {
					this.moveCell(3, -1, -1);
					
					rotation=3;
				}
				
				break;
			case 3:
				// Only check the one position since the others are already populated by this block
				if (this.isCellMoveLegal(3, 1, 1)) {
					// Swap everything back to the start position
					this.moveCell(3, 1, 1);
					this.moveCell(0, 1, -1);
					this.moveCell(1, -1, -1);
					
					rotation=0;
				}
				
				break;
		}
	}

}
